The primary objective of the Diel.Niche package is to be able to use observations of wild animals to estimate support for a set of diel niche hypotheses. The fundamental data analysis unit is a set of three frequencies (y): the number of observations of a species during twilight, daytime, and nighttime (in that order). In this vignette, we will demonstrate the basic functionality of the package. This includes 1) model comparison, 2) parameter estimation, 3) posterior predictive check, and 4) results visualization.

We will use data available in the package (‘diel.data’) provided by the Urban Wildlife Information Network (https://www.urbanwildlifeinfo.org/). The specific data are camera trap detections of the Virginia Opossum (Didelphis virginiana) during the winter of 2018. The available data are aggregated independent counts from 131 camera locations in Chicago, Illinois USA. Our objective is to evaluate the support for the traditional diel hypotheses of the Virginia Opossum in an urban environment during the winter.

The Setup

To start, we load the available packages and extract the specific data of interest.

# Load packages
  library(Diel.Niche)
  library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
  library(coda)
  library(ggplot2)
  library(bayesplot)
#> This is bayesplot version 1.10.0
#> - Online documentation and vignettes at mc-stan.org/bayesplot
#> - bayesplot theme set to bayesplot::theme_default()
#>    * Does _not_ affect other ggplot2 plots
#>    * See ?bayesplot_theme_set for details on theme setting

# Define a year variable
  diel.data$min_year = year(as.POSIXct(diel.data$min_date, format = "%m/%d/%Y"))

# Extract winter data in 2018 for the Virginia Opossum
  winter = subset(diel.data, season=="Winter" & min_year=="2018" & scientificName=="Didelphis virginiana")

# Visualize the data
  head(winter)
#>           scientificName twilight day night trap_nights nsite   min_date
#> 111 Didelphis virginiana        5  20    39        1616   131 12/29/2018
#>      max_date mean_lat mean_lon season       country   phylum    class
#> 111 1/24/2019 41.87236 -87.8423 Winter United States Chordata Mammalia
#>               order      family             Project unit_type      Common_name
#> 111 Didelphimorphia Didelphidae UWIN_Chicago_IL_USA     28day Virginia Opossum
#>     Activity_Literature min_year
#> 111           Nocturnal     2018

For simplicity, we extract the count data and assign it to the object y.

y=data.frame(twilight=winter$twilight,
             day=winter$day, 
             night=winter$night)
rownames(y)=winter$Common_name

y
#>                  twilight day night
#> Virginia Opossum        5  20    39

Model Comparison

We are now ready to compare models using the Traditional hypothesis set, which includes four models: diurnal, nocturnal, crepuscular, and Traditional Cathemeral. We can confirm that this is the hypothesis set of interest by plotting the set together as,

diel.plot(hyp=hyp.sets("Traditional"))

To fit our data (y), we use the function ‘diel.fit’, specifying the hypothesis set of interest.

out = diel.fit(y = as.matrix(y),
               hyp.set=hyp.sets("Traditional"))
#> Data checks Complete.
#> Calculating Bayes Factors...
#> The most supported model is: 
#>  Cathemeral (Traditional)

The most supported model is Traditional Cathemeral. We can examine the posterior model probabilities for all models as,

out$bf.table
#>    Prior Posterior
#> D   0.25         0
#> N   0.25         0
#> CR  0.25         0
#> C   0.25         1

which indicate that we are very certain that Traditional Cathemeral (‘C’) is supported. Notice that without specifying a prior probabilty for each model, it is assumed to be equal among the models.

Parameter Estimation

Next, lets fit the most supported model and get posterior distributions of our parameters. We use the ‘diel.fit’ function again, but this time specifying ‘post.fit = TRUE’. We can also change the the typical parameters when fitting a model using an MCMC algorithim: the number of chains, iterations, and burnin.

fit.ms = diel.fit(y = as.matrix(y),hyp.set=out$ms.model,
             post.fit = TRUE, 
             n.chains = 3, n.mcmc=5000,burnin = 1000)
#> Data checks Complete.
#> Posterior Sampling...

Checks

As long as we fit our model with multiple chains, we will get an estimate of the Gelman-Rubin convergence diagnostic, as

fit.ms$gelm.diag
#> $C
#> Potential scale reduction factors:
#> 
#>      Point est. Upper C.I.
#> p1_1          1          1
#> p1_2          1          1

Since the point estimate and upper intervals are at one, there is no evidence that the parameters have not converged to their posterior distributions. However, we should also plot the parameter chains to visually inspect this (next section).

We can also examine whether there is evidenve of lack of it betweem our most supported model and the data using a posterior predictive check, which provides a probability of fit (‘ppp’). As long as this value is not near 0 or 1 then there is no evidence of departure of fit to the data.

fit.ms$ppc
#>   Model   X2_obs  X2_pred       ppp
#> 1     C 1.988466 1.998308 0.5128333

This model indicates that it fits the data with a probability value of 0.51.

Visualizations

First, let’s plot our parameter chains to confirm that posterior distributions are properly converged.

plot(fit.ms$post.samp[[1]])

We see that the chains are highly overlapping and thus confirm that posterior distributions have converged.

Next, lets examine the posterior probabilities of activity in the three diel periods,

# Combine chains
  posteriors=do.call("rbind", fit.ms$post.samp[[1]])

#Setup plotting
p=apply(posteriors,2,median)
plot_title <- ggplot2::ggtitle("Posterior distributions",
                      "with medians and 95% intervals")
bayesplot::mcmc_areas(posteriors, prob = 0.95) + plot_title

Alternatively, we can use the diel.plot function in the Diel.Niche package that uses plotly to output a 3d plot of the posterior samples on top of the hypothesis set,

diel.plot(hyp=hyp.sets("Traditional"),
          posteriors=posteriors)

The black dots are the entire posterior samples plotted within the Traditional Cathemeral hypothesis. This point cloud shows us that the Virginia Opossum is largely active during night and day. To look at the posterior median and 95% credible intervals, we can extract these as,

apply(posteriors,2,quantile, probs=c(0.025,0.5,0.975))
#>         p_crep_1   p_day_1 p_night_1
#> 2.5%  0.03449378 0.2087697 0.4751622
#> 50%   0.08547342 0.3117323 0.5975055
#> 97.5% 0.16828159 0.4316335 0.7110955

Conclusions

What did we learn? We found that during the winter of 2018 in the urban environment of Chicago, IL that there is strong evidence that the Virginia Opossum is Cathemeral. Specifically, that the Opossum is active most during the nighttime and the daytime.